package com.esun.cheetah.view.setting;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.util.LoginTimeControl;

/**
 * 账户保护时间 com.esun.cheetah.view.setting.ChooseProTime
 * 
 * @author 任付江 create at 2012-4-28 下午2:30:06
 */
public class ChooseProTime extends Activity implements OnClickListener {
	/** 返回按钮 */
	private Button btBack;
	/** 保护CheckBox */
	private CheckBox cbProAccount;
	/** 时间EditText */
	private EditText etTime;
	/** 时间选择CheckBox */
	private Button btChooseTime;
	/** 是否开启选择时间按钮 */
	private boolean isChooseTime;
	private SharedPreferences sharedPrefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_protect_time);
		sharedPrefs = this.getSharedPreferences(Config.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		sharedPrefs.getString(Config.ACCOUNT_PROTECT_TIME_STRING, UserInfo.getUserInfo().getDefaultAccountTime());
		isChooseTime = UserInfo.getUserInfo().isAccountProct();
		initViews();
	}

	private void initViews() {
		btBack = (Button) findViewById(R.id.btnAboutback);
		btBack.setOnClickListener(this);
		cbProAccount = (CheckBox) findViewById(R.id.cbxSecurity);
		cbProAccount.setChecked(isChooseTime);
		btChooseTime = (Button) findViewById(R.id.btTime);
		if (!isChooseTime) {
			btChooseTime.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_disable_customtime_32x32));
		}
		cbProAccount.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isChooseTime = isChecked;
				UserInfo.getUserInfo().setAccountProct(isChecked);
				if (isChecked) {
					isChooseTime = true;
					btChooseTime.setBackgroundDrawable(getResources().getDrawable(R.drawable.time_selector_coder));
				} else {
					LoginTimeControl.createPassDialog(ChooseProTime.this, btChooseTime, cbProAccount);
				}
			};
		});
		etTime = (EditText) findViewById(R.id.etTime);
		etTime.setText(UserInfo.getUserInfo().getDefaultAccountTime());
		int i = 0;
		Long time = 0l;
		String str = etTime.getText().toString();
		String str1 = str.substring(0, 1);
		i = Integer.parseInt(str1);
		time = (long) (i * 60 * 10000);
		saveLongTimeToPreference(time);
		UserInfo.getUserInfo().setDurationTime(time);
		btChooseTime.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		// 返回
		case R.id.btnAboutback:
			finish();
			break;
		case R.id.btTime:
			if (isChooseTime) {
				createDialog();
			}
			break;
		default:
			break;
		}
	}

	// 用于保存时间
	@Override
	protected void onPause() {
		super.onPause();
		int i = 0;
		Long time = 0l;
		String str = etTime.getText().toString();
		String str1 = str.substring(0, 1);
		i = Integer.parseInt(str1);
		time = (long) (i*60*1000);
		if (!UserInfo.getUserInfo().isAccountProct()) {
			UserInfo.getUserInfo().setDurationTime(0);
			Toast.makeText(this, "关闭了账户保护，设置不生效", Toast.LENGTH_SHORT).show();
		} else {
			UserInfo.getUserInfo().setDurationTime(time);
		}

		UserInfo.getUserInfo().setAccountProct(isChooseTime);

	}

	/**
	 * 保存设置保护时间Long行到preferences
	 * 
	 * @param time
	 */
	private void saveLongTimeToPreference(Long time) {
		Editor editor = sharedPrefs.edit();
		editor.putLong(Config.ACCOUNT_PROTECT_TIME, time);
		editor.commit();
	}

	/**
	 * 保存设置保护时间 字符串 到Preference
	 * 
	 * @param time
	 */
	private void saveTimeStringToPreference(String time) {
		Editor editor = sharedPrefs.edit();
		editor.putString(Config.ACCOUNT_PROTECT_TIME_STRING, time);
		editor.commit();
	}

	/**
	 * 时间选择对话框
	 */
	public void createDialog() {
		final Dialog inputPwdDialog = new Dialog(this, R.style.dialog);
		inputPwdDialog.getWindow().setContentView(R.layout.time_choose_dialog);
		ListView lvTime = (ListView) inputPwdDialog.getWindow().findViewById(R.id.lvTime);
		Button btnCacel = (Button) inputPwdDialog.getWindow().findViewById(R.id.btnCancel);
		btnCacel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inputPwdDialog.cancel();

			}
		});
		TimeAdapter timeAdapter = new TimeAdapter(this, inputPwdDialog);
		lvTime.setAdapter(timeAdapter);
		inputPwdDialog.show();
	}

	/**
	 * 时间适配器 com.esun.cheetah.view.setting.TimeAdapter
	 * 
	 * @author 任付江 create at 2012-3-30 上午11:01:05
	 */
	public class TimeAdapter extends BaseAdapter {
		/** 上下文对象 */
		private Context context = null;
		/** 时间数组 */
		private String[] time = { "5分钟", "10分钟", "15分钟", "20分钟 ", "30分钟" };
		/** 时间 */
		private String strTime = null;
		/** 时间对话框 */
		private Dialog inputPwdDialog;

		public TimeAdapter(Context c, Dialog inputPwdDialog) {
			this.context = c;
			this.inputPwdDialog = inputPwdDialog;
		}

		@Override
		public int getCount() {
			return time.length;
		}

		@Override
		public Object getItem(int position) {
			return time[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			LayoutInflater lif = LayoutInflater.from(context);
			sharedPrefs.getString(Config.ACCOUNT_PROTECT_TIME_STRING, UserInfo.getUserInfo().getDefaultAccountTime());
			convertView = lif.inflate(R.layout.time_ckview, null);
			final CheckedTextView ctb = (CheckedTextView) convertView.findViewById(R.id.chtvTime);
			ctb.setText(time[position]);
			if (etTime.getText().toString().equals(time[position])) {
				ctb.setCheckMarkDrawable(context.getResources().getDrawable(R.drawable.btn_pressed_popup_radiobtn_32x32));
			}
			ctb.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					strTime = ctb.getText().toString();
					ctb.setCheckMarkDrawable(context.getResources().getDrawable(R.drawable.btn_pressed_popup_radiobtn_32x32));
					if (isChooseTime) {
						etTime.setText(strTime);
						UserInfo.getUserInfo().setDefaultAccountTime(strTime);
						saveTimeStringToPreference(strTime);
					}
					inputPwdDialog.cancel();
				}
			});
			return convertView;
		}
	}

}
